package com.mylib.libcore.dialog;

import android.app.Activity;
import android.content.Context;
import android.graphics.Rect;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.mylib.libcore.R;
import com.mylib.libcore.utils.ToastUtil;
import com.mylib.libcore.utils.UIUtils;


/**
 * 闯关成功弹框
 */
public class CommonDialogEdit extends PopupWindow {

    private EditText et_content;
    private TextView tv_sure,tv_cancel,tv_title;
    private View mMenuView;
    public CommonDialogEditBtnClink dialogViewClink;
    public interface  CommonDialogEditBtnClink{
        void btnClink(String text,String type,String id);
    }

    public CommonDialogEdit(Activity context, String title,String type,String id, CommonDialogEditBtnClink dialogViewClink) {
        super(context);
        this.dialogViewClink =dialogViewClink;
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mMenuView = inflater.inflate(R.layout.dialog_common_edit_layout, null);
        et_content = (EditText)mMenuView.findViewById(R.id.et_content);
        tv_title = (TextView) mMenuView.findViewById(R.id.tv_title);
        tv_sure = (TextView) mMenuView.findViewById(R.id.tv_sure);
        tv_cancel = (TextView) mMenuView.findViewById(R.id.tv_cancel);
        tv_title.setText(title+"");

        tv_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        tv_sure.setOnClickListener(v ->{
            finishhideSoftBoard(context);
            if(UIUtils.isEmpty(et_content.getText()) || et_content.getText().toString().trim().length()==0){
                ToastUtil.show("请填写");
                return;
            }
            if(type.equals("notepad_edit") || type.equals("notepad_type")){
                if(et_content.getText().toString().trim().length()>5){
                    ToastUtil.show("备忘名称不能超过5个字符");
                    return;
                }
            }
            dismiss();
            if(!UIUtils.isEmpty(dialogViewClink))dialogViewClink.btnClink(et_content.getText().toString().trim(),type,id);
        } );


        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            this.setClippingEnabled(false);
        }

        //设置SelectPicPopupWindow的View
        this.setContentView(mMenuView);
        //设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(LayoutParams.FILL_PARENT);
        //设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(LayoutParams.MATCH_PARENT);
        //设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
//        //设置SelectPicPopupWindow弹出窗体动画效果
//        this.setAnimationStyle(R.style.popwin_anim_style);
        //实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0xb0000000);
        //设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
    }


    //关闭软键盘
    public void finishhideSoftBoard(Activity context) {
        InputMethodManager imm = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);
        if(imm.isActive()&&context.getCurrentFocus()!=null){
            if (context.getCurrentFocus().getWindowToken()!=null) {
                imm.hideSoftInputFromWindow(context.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
            }
        }
    }

    @Override
    public void showAsDropDown(View anchor) {
        if(Build.VERSION.SDK_INT >= 24) {
            Rect rect = new Rect();
            anchor.getGlobalVisibleRect(rect);
            int h = anchor.getResources().getDisplayMetrics().heightPixels - rect.bottom;
            setHeight(h);
        }
        super.showAsDropDown(anchor);
    }

    @Override
    public void showAsDropDown(View anchor, int xoff, int yoff) {
        if(Build.VERSION.SDK_INT >= 24) {
            Rect rect = new Rect();
            anchor.getGlobalVisibleRect(rect);
            int h = anchor.getResources().getDisplayMetrics().heightPixels - rect.bottom;
            setHeight(h);
        }
        super.showAsDropDown(anchor, xoff, yoff);
    }

}